package cn.lu.train.map;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;

import java.util.ArrayList;
import java.util.HashMap;

public class BaiduMapView extends LinearLayout implements BaiduMap.OnMapClickListener{
    private Context context;

    //地图相关，使用继承MapView的MyRouteMapView目的是重写touch事件实现泡泡处理
    //如果不处理touch事件，则无需继承，直接使用MapView即可
    /**地图View */
    private MapView mMapView = null;
    private BaiduMap mBaidumap = null;

    //覆盖物相关
    private HashMap<String, Marker> markers= new HashMap<>();//保存覆盖物
    /** 覆盖物点击接口  */
    public interface OnMyMarkerClickListener{
        void onMarkerClick(Marker arg0);
    }
    private OnMyMarkerClickListener listener=null;
    public void setOnMarkerClickListener(OnMyMarkerClickListener listener){
        this.listener=listener;
    }

    public BaiduMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context=context;
        initBaiduMap();
    }

    /**初始化百度地图
     *
     */
    private void initBaiduMap(){
        Log.v("BaiduMapView", "initBaiduMap");
        //初始化地图
        mMapView =new MapView(context);
        mMapView.showZoomControls(false);//缩放按钮

        mBaidumap = mMapView.getMap();
        //地图点击事件处理
        mBaidumap.setOnMapClickListener(this);

//        mBaidumap.setOnMapLoadedCallback(new OnMapLoadedCallback() {
//
//          @Override
//          public void onMapLoaded() {
//              // TODO Auto-generated method stub
//              ToastUtils.showTextToast(SelectStationActivity.this, "地图加载完成");
//
//              //SearchButtonProcess();
//          }
//      });

        //定位
        BaiduLocation.getLocation(context);
        BaiduLocation.setMyLocationListener(new BaiduLocation.MyLocationListener(){
            @Override
            public void myLocatin(double mylongitude, double mylatitude, String city, String street) {
                // TODO Auto-generated method stub
                setCenter(new LatLng(mylatitude,mylongitude) );
            }
        });

        addView(mMapView);
    }

    /**设置中心点
     * @param latLng
     */
    public void setCenter(LatLng latLng){
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
        mBaidumap.animateMapStatus(u);
    }

    @Override
    public void onMapClick(LatLng arg0) {
        // TODO Auto-generated method stub
        mBaidumap.hideInfoWindow();
    }

    @Override
    public boolean onMapPoiClick(MapPoi arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    /**添加覆盖物
     * @param view
     * @param ll 经纬度坐标
     * @param title 覆盖物title，用于标识
     */
    public void addOverlay(View view, LatLng ll, String title) {
        // add marker overlay
        BitmapDescriptor bdC = BitmapDescriptorFactory.fromView(view);
        MarkerOptions ooC = new MarkerOptions().title(title).position(ll).icon(bdC).perspective(false).anchor(0.5f, 1f);
        ooC.animateType(MarkerOptions.MarkerAnimateType.grow);//生长动画
        markers.put(title,(Marker)(mBaidumap.addOverlay(ooC)));//此处可以强转成Marker，控制覆盖物移动

        mBaidumap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker arg0) {
                // TODO Auto-generated method stub
                if (listener!=null) {
                    listener.onMarkerClick(arg0);
                }
                return true;
            }
        });
    }
    /**设置覆盖物位置
     * @param ll 经纬度坐标
     */
    public void setOverlayLatLng(String title,LatLng ll){
        if (markers!=null) {
            Marker marker = markers.get(title);
            if (marker!=null) {
                marker.setPosition(ll);
            }
        }
    }

    /**发起路线规划搜索示例
     * @param start 起点
     * @param end 终点
     * @param passBy 途经点，仅在drivingSearch有效
     * @param state 选择搜索方式
     */
    public void SearchProcess(LatLng start, LatLng end, ArrayList<LatLng> passBy, int state) {
        BaiduSearch search = new BaiduSearch(context);
        search.setOnMyGetRoutePlanResultListener(new BaiduSearch.OnMyGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult arg0) {
                // TODO Auto-generated method stub
                WalkingRouteOverlay overlay = new WalkingRouteOverlay(mBaidumap);
//              mBaidumap.setOnMarkerClickListener(overlay);
                overlay.setData(arg0.getRouteLines().get(0));
                overlay.addToMap();
                overlay.zoomToSpan();
            }
            @Override
            public void onGetTransitRouteResult(TransitRouteResult arg0) {
                // TODO Auto-generated method stub
                TransitRouteOverlay overlay = new TransitRouteOverlay(mBaidumap);
//              mBaidumap.setOnMarkerClickListener(overlay);
                overlay.setData(arg0.getRouteLines().get(0));
                overlay.addToMap();
                overlay.zoomToSpan();
            }
            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult arg0) {
                // TODO Auto-generated method stub
                DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaidumap);
//              mBaidumap.setOnMarkerClickListener(overlay);
                overlay.setData(arg0.getRouteLines().get(0));
                overlay.addToMap();
                overlay.zoomToSpan();
            }
            @Override
            public void onGetBikingRouteResult(BikingRouteResult arg0) {
                // TODO Auto-generated method stub

            }
        });
        search.SearchProcess(start, end, passBy, state);
    }

    /**
     * 设置底图显示模式
     *
     * @param state 0:普通图 1：卫星图
     */
    public void setMapMode(int state) {
        switch (state) {
            case 0:
                mBaidumap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                break;
            case 1:
                mBaidumap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                break;
            default:
                break;
        }
    }

    /**
     * 设置是否显示交通图
     *
     * @param isTraffic
     */
    public void setTraffic(boolean isTraffic) {
        mBaidumap.setTrafficEnabled(isTraffic);
    }

    /**
     * 设置是否显示百度热力图
     *
     * @param isHeatMap
     */
    public void setBaiduHeatMap(boolean isHeatMap) {
        mBaidumap.setBaiduHeatMapEnabled(isHeatMap);
    }
}
